/**
 * Copyright (c) The openTCS Authors.
 *
 * This program is free software and subject to the MIT license. (For details,
 * see the licensing information (LICENSE.txt) you should have received with
 * this copy of the software.)
 */
package org.opentcs.operationsdesk.application.action.course;

import com.google.inject.assistedinject.Assisted;
import jakarta.inject.Inject;
import org.opentcs.access.KernelRuntimeException;
import org.opentcs.access.SharedKernelServicePortal;
import org.opentcs.access.SharedKernelServicePortalProvider;
import org.opentcs.customizations.plantoverview.ApplicationFrame;
import org.opentcs.guing.base.model.elements.VehicleModel;
import org.opentcs.thirdparty.guing.common.jhotdraw.util.ResourceBundleUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Collection;

import static java.util.Objects.requireNonNull;
import static org.opentcs.operationsdesk.util.I18nPlantOverviewOperating.VEHICLEPOPUP_PATH;


  public class FinishAction
      extends AbstractAction {
  /**
   * The ID for the 'withdraw regularly' action.
   */
  public static final String ID = "course.vehicle.withdrawTransportOrder";
  /**
   * The ID for the 'withdraw forcibly' action.
   */
  public static final String IMMEDIATELY_ID = "course.vehicle.withdrawTransportOrderImmediately";

  private static final ResourceBundleUtil BUNDLE = ResourceBundleUtil.getBundle(VEHICLEPOPUP_PATH);
  /**
   * This class's logger.
   */
  private static final Logger LOG = LoggerFactory.getLogger(FinishAction.class);
  /**
   * The vehicles.
   */
  private final Collection<VehicleModel> vehicles;
  /**
   * Provides access to a portal.
   */
  private final SharedKernelServicePortalProvider portalProvider;
  /**
   * The parent component for dialogs shown by this action.
   */
  private final Component dialogParent;

  /**
   * Creates a new instance.
   *
   * @param vehicles The selected vehicles.
   * @param portalProvider Provides access to a shared portal.
   * @param dialogParent The parent component for dialogs shown by this action.
   */
  @Inject
  public FinishAction(@Assisted Collection<VehicleModel> vehicles,
                      SharedKernelServicePortalProvider portalProvider,
                      @ApplicationFrame Component dialogParent) {
    this.vehicles = requireNonNull(vehicles, "vehicles");
    this.portalProvider = requireNonNull(portalProvider, "portalProvider");
    this.dialogParent = requireNonNull(dialogParent, "dialogParent");
    putValue(NAME, BUNDLE.getString("finishAction.make_sure.name"));
  }

  @Override
  public void actionPerformed(ActionEvent evt) {
    try (SharedKernelServicePortal sharedPortal = portalProvider.register()) {
      for (VehicleModel vehicle : vehicles) {
        sharedPortal.getPortal().getDispatcherService().finishByVehicle(
            vehicle.getVehicle().getReference());
      }
    }
    catch (KernelRuntimeException e) {
      LOG.warn("Unexpected exception", e);
    }
  }
  }
